package com.yeyks.app.controller.base.hotel;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.yeyks.app.annotation.LoadUserId;
import com.yeyks.common.ajax.AjaxResponseBody;
import com.yeyks.common.convert.DateEditor;
import com.yeyks.common.exception.ServiceException;
import com.yeyks.commonReference.service.hotel.HotelSearchC2bService;
import com.yeyks.hotel.param.GrabTheOrderServiceGetNowGrabTheOrderedHotelListParam;
import com.yeyks.hotel.param.GrabTheOrderServiceGrabTheOrderParam;
import com.yeyks.hotel.vo.GrabTheOrderServiceGrabTheOrderVo;
import com.yeyks.hotel.vo.HotelInfoHotelSearchVo;
import com.yeyks.search.pojo.param.search.HotelSearchC2bListParam;
import com.yeyks.search.pojo.param.search.HotelSearchC2bOrderCalendarParam;
import com.yeyks.search.pojo.vo.search.HotelInfoHotelUnionSearchVo;
import com.yeyks.search.pojo.vo.search.HotelInfoHotelUnionSearchVoHotle;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Date;
import java.util.Objects;

/**
 * @Author fuchangshuai
 * @date 2019/3/26 4:27 PM
 */
@Api(tags = "酒店搜索c2b接口", description = "酒店搜索c2b接口")
@Slf4j
@RestController
@RequestMapping(value = "/app/hotel")
public class HotelSearchC2bController {

    @Autowired
    private HotelSearchC2bService hotelSearchC2bService;

    @InitBinder
    public void initbinder(WebDataBinder webDataBinder) {
        webDataBinder.registerCustomEditor(Date.class, new DateEditor());
    }

    //v3.4
    @ApiOperation(value = "抢单:发起抢单:v4.0.0")
    @RequestMapping(value = "/c2b/hotelGrabTheOrderSearch", method = RequestMethod.POST)
    public AjaxResponseBody<GrabTheOrderServiceGrabTheOrderVo> hotelGrabTheOrderSearch(@Validated @LoadUserId GrabTheOrderServiceGrabTheOrderParam param) {
        //发送抢单给商家端服务器
        GrabTheOrderServiceGrabTheOrderVo vo = hotelSearchC2bService.grabTheOrder(param);
        return AjaxResponseBody.success(vo);
    }

    //v3.4
    @Deprecated
//    @ApiIgnore
//    @ApiOperation(value = "抢单:获取已抢单的列表:v3.4")
//    @RequestMapping(value = "/c2b/getNowAllGrabTheOrderedHotelList", method = RequestMethod.POST)
    public AjaxResponseBody<HotelInfoHotelSearchVo> getNowAllGrabTheOrderedHotelList(@Validated @LoadUserId GrabTheOrderServiceGetNowGrabTheOrderedHotelListParam param) {
        //发送抢单给商家端服务器
        HotelInfoHotelSearchVo vo = hotelSearchC2bService.getNowAllGrabTheOrderedHotelListVo(param);
        return AjaxResponseBody.success(vo);
    }

    //v3.6
    //接入cn
    @ApiOperation(value = "抢单:获取已抢单的列表:v3.6")
    @RequestMapping(value = "/c2b/getGrabTheOrderedHotelList", method = RequestMethod.POST)
    public AjaxResponseBody<HotelInfoHotelUnionSearchVo> getGrabTheOrderedHotelList(@Validated @LoadUserId HotelSearchC2bListParam param) {
        if (Objects.isNull(param.getSort())) {
            param.setSort(1);
        }
        return AjaxResponseBody.success(hotelSearchC2bService.getGrabTheOrderedHotelList(param));
    }

    //v3.6
    //接入cn
    @ApiOperation(value = "C2B订单房态信息:v3.6")
    @RequestMapping(value = "/c2b/c2bHotelOrderCalendar", method = RequestMethod.POST)
    public AjaxResponseBody<HotelInfoHotelUnionSearchVoHotle> c2bHotelOrderCalendar(@Validated @LoadUserId HotelSearchC2bOrderCalendarParam param) {
        HotelSearchC2bListParam hotelSearchC2bListParam = BeanUtil.toBean(param, HotelSearchC2bListParam.class);
        HotelInfoHotelUnionSearchVo theOrderedHotelList = hotelSearchC2bService.getGrabTheOrderedHotelList(hotelSearchC2bListParam);
        if (Objects.isNull(theOrderedHotelList) || CollectionUtil.isEmpty(theOrderedHotelList.getList())) {
            throw new ServiceException("房态信息有误");
        }
        return AjaxResponseBody.success(theOrderedHotelList.getList().get(0));
    }

}
